<template>
  <div class="login">
    <van-nav-bar title="登录" left-arrow @click-left="$router.back()" />

    <!-- 登录表单  点击登录按钮的时候，会触发submit事件 -->
    <van-form @submit="onSubmit">
      <van-field
        :rules="rules.mobile"
        required
        v-model="mobile"
        label="手机号"
        placeholder="请输入你的手机号"
      />
      <van-field
        :rules="rules.code"
        required
        v-model="code"
        label="验证码"
        placeholder="请输入验证码"
      />
      <div style="margin: 16px">
        <van-button
          :disabled="isLoading"
          :loading="isLoading"
          loading-text="登录中"
          round
          block
          type="info"
          native-type="submit"
        >
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { userLogin } from '@/api/user'

export default {
  name: 'Login',
  data() {
    return {
      isLoading: false,
      mobile: '13856575621',
      code: '246810',
      rules: {
        // 手机号的校验规则
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            // 手机号的格式 正则
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      }
    }
  },
  methods: {
    onSubmit(){
    async  const res = await axios({
        method:'POST',
        url:"/v1_0/authorizations",
        data:{
          mobile:this.mobile,
          code:this.code
        }
      })
      console.log(res);
    }
  }

  // methods: {
  //   // 不能给提交按钮注册click发送请求 ==> 表单校验不通过的时候，也会触发
  //   async onSubmit() {
  //     // console.log('登录中')
  //     // 发送请求时修改loading和禁用状态
  //     this.isLoading = true

  //     try {
  //       const res = await userLogin(this.mobile, this.code)
  //       console.log(res)

  //       // 现在需要把token存vue中
  //       this.$store.commit('setToken', res.data)

  //       this.$toast.success('登录成功')
  //       this.$router.push('/')
  //     } catch {
  //       this.$toast.success('登录失败')
  //     }
  //     // 发送请求时修改loading和禁用状态
  //     this.isLoading = false
  //   }
  // }
}
</script>

<style lang="less" scoped></style>
